package com.mpgame.database.mysql.service.game;

import com.mpgame.database.mysql.mapper.game.MonsterStoreMapper;
import com.mpgame.database.mysql.model.game.MonsterStore;
import com.mpgame.database.mysql.utils.MyBatisUtil;
import com.mpgame.utils.Log;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MonsterStoreService {
    
    public int createMonsterStore(MonsterStore monsterStore) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterStoreMapper mapper = sqlSession.getMapper(MonsterStoreMapper.class);
            int res = mapper.insert(monsterStore);
            sqlSession.commit();
            
            if (res > 0) {
                Log.info("创建怪物仓库条目成功: " + monsterStore.getId());
                return monsterStore.getId();
            } else {
                Log.err("创建怪物仓库条目失败");
                return 0;
            }
        } catch (Exception e) {
            Log.err("创建怪物仓库条目异常: " + e.getMessage());
            throw new RuntimeException("创建怪物仓库条目异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public MonsterStore getMonsterStoreById(int id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterStoreMapper mapper = sqlSession.getMapper(MonsterStoreMapper.class);
            return mapper.selectById(id);
        } catch (Exception e) {
            Log.err("获取怪物仓库条目失败: " + e.getMessage());
            throw new RuntimeException("获取怪物仓库条目异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public List<MonsterStore> getMonsterStoresByPlayerId(int playerId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterStoreMapper mapper = sqlSession.getMapper(MonsterStoreMapper.class);
            return mapper.selectByPlayerId(playerId);
        } catch (Exception e) {
            Log.err("根据玩家ID获取怪物仓库失败: " + e.getMessage());
            throw new RuntimeException("根据玩家ID获取怪物仓库异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public List<MonsterStore> getMonsterStoresByMonsterId(int monsterId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterStoreMapper mapper = sqlSession.getMapper(MonsterStoreMapper.class);
            return mapper.selectByMonsterId(monsterId);
        } catch (Exception e) {
            Log.err("根据怪物ID获取仓库失败: " + e.getMessage());
            throw new RuntimeException("根据怪物ID获取仓库异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public boolean updateMonsterStore(MonsterStore monsterStore) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            MonsterStoreMapper mapper = sqlSession.getMapper(MonsterStoreMapper.class);
            int res = mapper.updateById(monsterStore);
            sqlSession.commit();
            return res > 0;
        } catch (Exception e) {
            Log.err("更新怪物仓库条目失败: " + e.getMessage());
            throw new RuntimeException("更新怪物仓库条目异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
